/**   
 * @Title: MatchInfoCommunicater.java 
 * @Package com.esun.cheetah.biz.match.matchinfo
 * @Description: 通讯相关
 * @author 程相亭 <br/>
 * @date 2012-03-19 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.biz.match.matchinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;

import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.model.match.matchinfo.MatchInfoItem;
import com.esun.cheetah.model.match.matchinfo.MatchInfoRealTimeBean;
import com.esun.cheetah.network.AbsInferiorityCommunicater;

/**
 * @ClassName: MatchInfoCommunicater
 * @Description:
 * @author 程相亭 <br/>
 * @date 2012-03-14 下午05:07:33
 */
public class MatchInfoCommunicater extends AbsInferiorityCommunicater {

	// 实况信息存放数据结构
	private MatchInfoRealTimeBean matchInfoRealTimeBean;

	private String matchId = "";

	public MatchInfoCommunicater(Handler handler, String matchId) {
		super(handler);
		this.matchId = matchId;
		// matchInfoRealTimeBean = MatchInfoRealTimeBean.getInstance() ;
		matchInfoRealTimeBean = new MatchInfoRealTimeBean();
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> hmJson = new HashMap<String, String>();
		hmJson.put(Key.KEY_ID, BizCode.BIZCODE_MATCH_INFO);
		hmJson.put(Key.KEY_MATCH_ID, this.matchId);
		if (true) {
			hmJson.put(Key.KEY_USERNAME, UserInfo.getUserInfo().getUserName());
		}
		return hmJson;
	}

	/**
	 * @throws JSONException
	 * @Title: getJsonContent
	 * @Description: 解析JSON
	 * @param
	 * @return Object
	 * @throws
	 */
	@Override
	public void getJsonContent(String serverRes) throws JSONException {

		JSONObject jsonObject = new JSONObject(serverRes);
		if (!jsonObject.isNull(Key.KEY_DATA)) {
			jsonObject = jsonObject.getJSONObject(Key.KEY_DATA);

			JSONObject joMatchInfo = jsonObject.getJSONObject(Key.KEY_MATCH);

			// 比赛状态
			matchInfoRealTimeBean.status = joMatchInfo
					.getString(Key.KEY_STATUS);
			if (joMatchInfo.has(Key.KEY_STARTTIME)) {
				matchInfoRealTimeBean.startTime = joMatchInfo
						.getString(Key.KEY_STARTTIME);
			} else {
				matchInfoRealTimeBean.startTime = "1900-01-01 00:00:00";
			}
			matchInfoRealTimeBean.currentTime = joMatchInfo
					.getString(Key.KEY_NOWTIME);
			matchInfoRealTimeBean.lastTime = joMatchInfo
					.getString(Key.KEY_TIMEING);
			matchInfoRealTimeBean.isGuanzhu = joMatchInfo
					.getString(Key.KEY_ISGUANZHU);

			matchInfoRealTimeBean.simplegbName = joMatchInfo
					.getString(Key.KEY_SIMPLE_GB_NAME);
			matchInfoRealTimeBean.homeStanding = joMatchInfo
					.getString(Key.KEY_HOME_STANDING);
			matchInfoRealTimeBean.awayStanding = joMatchInfo
					.getString(Key.KEY_AWAY_STANDING);
			matchInfoRealTimeBean.matchRound = joMatchInfo
					.getString(Key.KEY_MATCH_ROUND);
			matchInfoRealTimeBean.stageName = joMatchInfo
					.getString(Key.KEY_STAGE_NAME);
			matchInfoRealTimeBean.stageMode = joMatchInfo
					.getString(Key.KEY_STAGE_MODE);
			matchInfoRealTimeBean.homeTeamId = joMatchInfo
					.getString(Key.KEY_HOME_TEAM_ID);
			matchInfoRealTimeBean.awayTeamId = joMatchInfo
					.getString(Key.KEY_AWAY_TEAM_ID);

			// 主客队信息
			String[] homeInfoArr = (joMatchInfo.getString(Key.KEY_HOME))
					.split(Language.SPEC_COMMA);
			String[] guestInfoArr = (joMatchInfo.getString(Key.KEY_AWAY))
					.split(Language.SPEC_COMMA);

			matchInfoRealTimeBean.homeName = homeInfoArr[0];
			matchInfoRealTimeBean.homeScore = homeInfoArr[1];
			matchInfoRealTimeBean.homeHalfScore = homeInfoArr[2];

			matchInfoRealTimeBean.guestName = guestInfoArr[0];
			matchInfoRealTimeBean.guestScore = guestInfoArr[1];
			matchInfoRealTimeBean.guestHalfScore = guestInfoArr[2];

			// 队标
			matchInfoRealTimeBean.homeSign = Config.LOGO_ADDRESS
					+ ((joMatchInfo.getString(Key.KEY_HOMESIGN))
							.split(Language.SPEC_QUOTATION_MARKS_POINT_CHANGE))[0]
					+ Language.SPEC_QUOTATION_MARKS_POINT_PNG;
			matchInfoRealTimeBean.guestSign = Config.LOGO_ADDRESS
					+ ((joMatchInfo.getString(Key.KEY_AWAYSIGN))
							.split(Language.SPEC_QUOTATION_MARKS_POINT_CHANGE))[0]
					+ Language.SPEC_QUOTATION_MARKS_POINT_PNG;

			// 技术统计
			JSONArray jsonMatchArray = jsonObject.getJSONArray(Key.KEY_STAT);
			if (!jsonMatchArray.isNull(0)) {
				joMatchInfo = jsonMatchArray.optJSONObject(0);
				// 主队技术统计
				matchInfoRealTimeBean.homeTechnicalStatistics = joMatchInfo
						.getString(Key.KEY_HOME);
				// 客队技术统计
				matchInfoRealTimeBean.guestTechnicalStatistics = joMatchInfo
						.getString(Key.KEY_AWAY);
			}

			// 比赛事件
			jsonMatchArray = jsonObject.getJSONArray(Key.KEY_INFO);

			List<MatchInfoItem> matchInfoItems = new ArrayList<MatchInfoItem>();
			int jsonSize = jsonMatchArray.length();

			for (int i = 0; i < jsonSize; i++) {
				MatchInfoItem matchInfoItem = new MatchInfoItem();
				joMatchInfo = jsonMatchArray.getJSONObject(i);

				// 比赛事件
				String[] matchEventArr = (joMatchInfo.getString(Key.KEY_DATA))
						.split(Language.SPEC_COMMA);
				matchInfoItem.timePoint = matchEventArr[0];
				matchInfoItem.eventType = matchEventArr[1];
				matchInfoItem.guestOrHome = matchEventArr[2];
				matchInfoItem.eventer = joMatchInfo.getString(Key.KEY_CDATA);

				matchInfoItems.add(matchInfoItem);
			}

			matchInfoRealTimeBean.matchInfoItems = matchInfoItems;
		}

	}

	public MatchInfoRealTimeBean getMatchInfoRealTimeBean() {
		return this.matchInfoRealTimeBean;
	}

	public void setMatchInfoRealTimeBean(
			MatchInfoRealTimeBean matchInfoRealTimeBean) {
		this.matchInfoRealTimeBean = matchInfoRealTimeBean;
	}

}
